import * as ActionTypes from './type'
import router from '@/router'
import * as Functions from '@/core/functions.js'

export default {
  // 获取站点初始信息
  [ActionTypes.NAV_DELETE]: (state, data) => {
    // 要删除path
    let path = data.path
    if (!path) {
      return
    }
    // 当前的path：currentPath
    let currentPath = state.nav.index
    let isPush = currentPath === path
    if (currentPath !== path) {
      // 删除的是其他的导航
      // 直接在 nav.list 中删除要删除的path
      let list = Functions.deletePathInList(state.nav.list, path)
      // 赋值
      state.nav.list = list
      return
    }
    // 找出另外一个当前的菜单并且要删除的是当前的导航
    currentPath = Functions.findOtherCurrentPath(state.nav.list, path)
    let nav = {
      index: currentPath,
      list: Functions.deletePathInList(state.nav.list, path)
    }
    state.nav = null
    state.nav = nav

    let item = getMenuIndex(state.nav)
    if (item) {
      state.menu = null
      let menu = { index: item }
      state.menu = menu
      if (!isPush) {
        router.push(path)
      }
    }

    router.push(currentPath)
  },
  // 点击改变当前菜单
  [ActionTypes.NAV_CHANGE]: (state, data) => {
    let path = data.path
    if (!path || path === state.nav.path) {
      return
    }
    let currentPath = state.nav.index
    let isPush = currentPath === path
    let nav = { index: path, list: JSON.parse(JSON.stringify(state.nav.list)) }
    state.nav = null
    state.nav = nav

    let item = getMenuIndex(state.nav)
    if (item) {
      state.menu = null
      let menu = { index: item }
      state.menu = menu
      if (!isPush) {
        router.push(path)
      }
    }
  },
  // 添加或者选中菜单
  [ActionTypes.NAV_APPEND]: (state, data) => {
    let path = data.path
    if (!path) {
      return
    }
    state.nav.index = path
    // 关闭同域名不同参数的菜单
    let pathq = data.path.split('?')
    if (pathq && pathq[0]) {
      let newnav = state.nav.list.filter(item => {
        if (item.path === path) {
          return item
        }
        let navpath = item.path.split('?')
        if (navpath && navpath[0] && navpath[0] !== pathq[0]) {
          return item
        }
      })
      state.nav.list = newnav
    }

    // 查找list当中是否存在 path
    if (!Functions.pathIsExistInNavList(state.nav.list, path)) {
      // 直接修改当前的 path
      let list = JSON.parse(JSON.stringify(state.nav.list))
      let menu = Functions.getMenuItemByPath(path)
      if (list.length > 9) {
        list.splice(1, 1)
      }
      list.push({
        menuIndex: menu.index,
        name: Functions.getNavNameByPath(menu, path),
        path: path
      })
      state.nav.list = JSON.parse(JSON.stringify(list))
    }
    router.push(path)
  },
  // 添加或者选中菜单并删除另外一个菜单
  [ActionTypes.NAV_APPEND_AND_CLOSE]: (state, data) => {
    let path = data.path
    let close = removeURLParameter(data.close, 'jsessionid')
    if (!path || !close) {
      return
    }
    state.nav.index = path

    // 删除当中的 要关闭的导航菜单
    let navList = JSON.parse(JSON.stringify(state.nav.list))
    navList = navList.filter(item => {
      return !(item.path === close)
    })
    state.nav.list = JSON.parse(JSON.stringify(navList))

    // 查找list当中是否存在 path
    if (!Functions.pathIsExistInNavList(state.nav.list, path)) {
      // 直接修改当前的 path
      let list = JSON.parse(JSON.stringify(state.nav.list))
      let menu = Functions.getMenuItemByPath(path)
      if (list.length > 9) {
        list.splice(1, 1)
      }
      list.push({
        menuIndex: menu.index,
        name: Functions.getNavNameByPath(menu, path),
        path: path
      })
      state.nav.list = JSON.parse(JSON.stringify(list))
    }
    router.push(path)
  },
  [ActionTypes.NAV_PREPEND_NAME_BY_PATH]: (state, data) => {
    // 关闭同域名不同参数的菜单
    let pathx = removeURLParameter(data.path, 'jsessionid')
    let pathq = data.path.split('?')
    if (pathq && pathq[0]) {
      let newnav = state.nav.list.filter(item => {
        if (item.path === pathx) {
          return item
        }
        let navpath = item.path.split('?')
        if (navpath && navpath[0] && navpath[0] !== pathq[0]) {
          return item
        }
      })
      state.nav.list = newnav
    }
    // 菜单添加跳转
    let list = Functions.prependNameByPath(data, state.nav.list)
    state.nav.list = list
  },
  // 点击返回改变当前菜单
  [ActionTypes.NAV_BACK_CHANGE]: (state, data) => {
    let path = data.path
    if (!path) {
      return
    }
    // 查找list当中是否存在 path
    if (!Functions.pathIsExistInNavList(state.nav.list, path)) {
      // 直接修改当前的 path
      let list = JSON.parse(JSON.stringify(state.nav.list))
      let menu = Functions.getMenuItemByPath(path)
      if (list.length > 9) {
        list.splice(1, 1)
      }
      list.push({
        menuIndex: menu.index,
        name: Functions.getNavNameByPath(menu, path),
        path: path
      })
      state.nav.list = JSON.parse(JSON.stringify(list))
    }
    let item = getMenuIndex(state.nav)
    if (item) {
      state.menu = null
      let menu = { index: item }
      state.menu = menu
    }
  }
}

const getMenuIndex = nav => {
  let index = nav.index
  for (let i in nav.list) {
    let item = nav.list[i]
    if (item.path === index) {
      return item.menuIndex
    }
  }
  return false
}

const removeURLParameter = (url, parameter) => {
  let urlparts = url.split('?')
  if (urlparts.length >= 2) {
    let prefix = encodeURIComponent(parameter) + '='
    let pars = urlparts[1].split(/[&;]/g)
    for (let i = pars.length; i-- > 0;) {
      if (pars[i].lastIndexOf(prefix, 0) !== -1) {
        pars.splice(i, 1)
      }
    }
    return urlparts[0] + (pars.length > 0 ? '?' + pars.join('&') : '')
  }
  return url
}
